package main

import (
	"encoding/json"
	"fmt"
	"main/config"
	"main/project"
	"os"
	"path/filepath"
)

func main() {
}

func startupDb() {
	config.InitDB()
}

func startInitEmptyProject() {
	root, _ := os.Getwd()
	workspace := "Workspace"
	target := filepath.Join(root, workspace)
	err := project.InitEmptyProject(target, "auto-studio-project-1")
	if err != nil {
		fmt.Printf("InitEmptyProjectErr : %v \n", err)
	}
}

func startLoadProject() {
	root, _ := os.Getwd()
	workspace := "Workspace"
	target := filepath.Join(root, workspace, "auto-studio-project-1")
	// load all node
	rootNode, loadErr := project.Load(target)
	if loadErr == nil {
		if jsonData, convertErr := json.Marshal(rootNode); convertErr == nil {
			fmt.Printf("root Node  : %v \n", string(jsonData))
		}
	}
	fmt.Printf("---------------------------------------\n")
	// query node
	nodeId := rootNode.ChildNodes[1].ChildNodes[1].NodeId
	queryNode := project.QueryNode(&rootNode, nodeId)
	if jsonData, convertErr := json.Marshal(queryNode); convertErr == nil {
		fmt.Printf("query Node  : %v \n", string(jsonData))
	}
	fmt.Printf("---------------------------------------\n")
	// remove node
	if project.RemoveNode(&rootNode, nodeId) {
		if jsonData, convertErr := json.Marshal(rootNode); convertErr == nil {
			fmt.Printf("after remove, root Node  : %v \n", string(jsonData))
		}
	}
	fmt.Printf("---------------------------------------\n")
	// update node
	updateId := rootNode.ChildNodes[1].ChildNodes[2].NodeId
	newNode := project.Node{
		NodeId:     "1",
		NodeName:   "1",
		NodeType:   project.NodeDir,
		NodePath:   "",
		ChildNodes: nil,
		IsHidden:   true,
	}
	if project.UpdateNode(&rootNode, updateId, &newNode) {
		if jsonData, convertErr := json.Marshal(rootNode); convertErr == nil {
			fmt.Printf("after update, root Node  : %v \n", string(jsonData))
		}
	}
}

func startMemDb() {
	project.CreateDB()
}

func startLoadProperties() {
	root, _ := os.Getwd()
	workspace := "Workspace"
	target := filepath.Join(root, workspace, "config", "sort_factor.properties")
	p := config.LoadProperties(target)
	order := p.GetInt("\\case\\case1\\main.py", 1)
	if order != 10 {
		fmt.Printf("LoadProperties err, can not get correct value")
	}
}
